<?php
/**
 * 现金卡 ajax类
 * @package  ajax
 * @subpackage  company
 * @author   create by allen.qiang
 */

quanli_require('module/base/company/cardFaceValue.class.php');

class ajax_cashCard{

    public function execute() {

        if(isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])){

            switch($_REQUEST['operFlg']){

                case 1://现金卡列表
                    $this->cashCardList();
                    break;

                case 2://查询现金卡
                    $this->searchCashCard();
                    break;

                case 100:
                    if (!isset($_REQUEST['act']) || !in_array($_REQUEST['act'], array('del', 'edit','change'))) {

                        returnJsonData('参数错误');

                    }

                    if ($_REQUEST['act'] == 'edit') {

                        $this->edit();//新增、修改 现金卡

                    } else if ($_REQUEST['act'] == 'del') {

                        $this->deleteCard();//删除现金卡

                    }

                    break;

                default:

                    returnJsonData('参数错误');

                    break;
            }

        }else {
            returnJsonData('参数错误');
        }
    }

    /**
     * 现金卡列表
     */
    private function cashCardList(){

        global $G_PGLOB;

        //实例化现金卡类
        $cardFaceValue = new cardFaceValue();
        $arg_array = $_REQUEST;
        $result = $cardFaceValue->search( $arg_array );

        //组织返回的数据
        $result_array = array(
            'page' => $arg_array['page'],
            'total' => $arg_array['count'],
            'rows' => array()
        );

        if ($result) {

            $edit_permission = $G_PGLOB->has_permission(70003);

            foreach($result as &$data) {

                if ($edit_permission) {



                    $operation_html = '';
                    $operation_html .= '<a href="javascript:void(0);" onclick="cardList.edit.open(' . $data['face_value_id'] . ');">修改</a>&nbsp;';

                    $result_array['rows'][] = array (
                        'id' => $data['face_value_id'],
                        'cell' => array(
                            $operation_html,
                            $data['face_value_id'],
                            $data['face_value'],
                            $data['card_info'],

                        )
                    );

                } else {

                    $operation_html = '';
                    $result_array['rows'][] = array (
                        'id' => $data['face_value_id'],
                        'cell' => array(
                            $operation_html,
                            $data['face_value_id'],
                            $data['face_value'],
                            $data['card_info'],

                        )
                    );

                }

            }

        }


        echo json_encode($result_array);
    }

    /**
     * 新增/修改  现金卡
     */
    private function edit() {

        global $G_SHOP;

        //实例代金卡类
        $cardFaceValue = new cardFaceValue();

        //新增/修改
        $arg_array = $_REQUEST;
        $arg_array['creator'] = $G_SHOP->currentUserID;
        $result_array = $cardFaceValue->editCashCard( $arg_array );
        returnJsonData($result_array['message'], $result_array['code']);

    }

    /**
     * 查询代金卡方式
     */
    private function searchCashCard() {

        if (!isset($_REQUEST['face_value_id']) || !quanli_is_int($_REQUEST['face_value_id'])) {
            returnJsonData('参数错误');
        }

        //实例现金卡类
        $cardFaceValue = new cardFaceValue();

        //查询现金卡方式
        $arg_array = array(
            'face_value_id' => $_REQUEST['face_value_id']
        );
        $card_array = $cardFaceValue->search( $arg_array );

        if ($card_array) {
            returnJsonData('ok', 200, $card_array[0]);
        } else {
            returnJsonData('现金卡信息没有找到', 404);
        }

    }

    /**
     * 删除现金卡
     */
    private function deleteCard () {

        //实例现金卡类
        $cardFaceValue = new cardFaceValue();

        //批量删除现金卡
        $result_array = $cardFaceValue->deleteCashCard( $_REQUEST['p_array'] );

        returnJsonData( $result_array['message'], $result_array['code'] );

    }

}
